package com.asyf.provider.common.config;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.asyf.provider.common.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    public GlobalExceptionHandler() {
        log.info("GlobalExceptionHandler实例化");
    }

    @ExceptionHandler(value = {Exception.class})
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public Result<String> handle(Exception e) {
        String localizedMessage = e.getLocalizedMessage();
        log.error(ExceptionUtil.stacktraceToString(e));
        return Result.fail(localizedMessage);
    }

}
